$(function() {
	$(".time  .timeBtn").click(function() {// 时间
		if (!$(this).hasClass("yes")) {

			$(".time  .timeBtn.yes").removeClass("yes");
			$(this).addClass("yes");
			if ($(this).attr("data-time") != "") {

				setQtime(parseInt($(this).attr("data-time")));// 设置日区间时间
			}
			if (!$(this).hasClass("custom")) {
				if ($(this).closest(".time").find(".custom").length > 0)
					$(".screen-box .customTime").hide();
			} else {
				$(".screen-box .customTime").show();
				// $("#dtp_input2").closest(".form_date").find(".delete").click();
				// $("#dtp_input1").closest(".form_date").find(".delete").click();
				$("#dtp_input2").val("");
				$("#dtp_input1").val("");
			}
		}
	});
	$("#refreshBtn").click(function() {
		statistics($("#dtp_input1").val(), $("#dtp_input2").val());

	});
	var setQtime = function(a) {// 日期设置
		var oDate = new Date();
		// var vdate=addDate(oDate,a*-1);
		// $("#date_2").val(addDate(oDate,0));
		// $("#date_1").val(addDate(oDate,a*-1));
		$("#dtp_input2").val(addDate(oDate, 0));
		$("#dtp_input1").val(addDate(oDate, a * -1));
		var s = addDate(oDate, 0);
		var e = addDate(oDate, a * -1);

		if (typeof (eval(statistics)) == "function") {
			statistics(addDate(oDate, a * -1),addDate(oDate, 0));

		}

		// $("#actionForm").submit();
		// statistics(addDate(oDate, 0), addDate(oDate, a * -1));
	};

	var addDate = function(date, days) {// 日期返回
		var d = new Date(date);
		d.setDate(d.getDate() + days);
		var month = d.getMonth() + 1;
		var day = d.getDate();
		if (month < 10) {
			month = "0" + month;
		}
		if (day < 10) {
			day = "0" + day;
		}
		var val = d.getFullYear() + "-" + month + "-" + day;
		return val;
	};

	var companyName = "";
	// 公司查询

	$(".companyAdd").click(function() {
		// if(!$(this).hasClass("no")){
		$(".companySearch").show(50);
		// }
	});
	$(".companySearch .close").click(function() {
		$(".companySearch").hide(50);
	});

	$("#keywordBtn")
			.click(
					function() {
						if ($("input[name='keyword']").length > 0) {

							// ajax
							$
									.ajax({
										url : '/getshops.htm',
										type : 'POST',
										dataType : 'json',
										data : {
											keyword : $("input[name='keyword']")
													.val()
										},
										success : function(data) {// [{"code":1,"name":"手机"},{"code":2,"name":"手机2"}]
											if (data) {
												$(".companySearch-con")
														.html("");
												$
														.each(
																data.shops,
																function(i,
																		item) {
																	$(
																			".companySearch-con")
																			.append(
																					"<div class='companySearch-box' data-code='"
																							+ item.code
																							+ "'><span class='sp1'>"
																						
																							+ item.name
																							+ "</span><span class='sp2'></span></div>");
																});
												
												companyWork();
											}
										}
									});
						}
					});
	
	var companyWork = function() {
		$(".companySearch-con .companySearch-box .sp2").unbind()
				.click(
						function() {
							if ($(".company-box .company").length >= 5) {
								$( "#dialog" ).html("");
								$( "#dialog" ).html("<p>最多分析5家企业</p>");
								$( "#dialog" ).dialog();
								
							}
							if ($(".company-box .company[data-code='"
									+ $(this).closest("div").attr("data-code")
									+ "']").length == 0
									&& $(".company-box .company").length < 5) {
								$(".company-box .companyAdd").before(
										"<div class='company' title='"
												+ $(this).closest("div").find(
														".sp1").text()
												+ "' data-code='"
												+ $(this).closest("div").attr(
														"data-code")
												+ "'>"
												+ $(this).closest("div").find(
														".sp1").text()
												+ "<i></i></div>");
								// if($(".company-box .company").length==5)
								// $(".company-box .companyAdd").addClass("no");
								//if($(".company-box .company").length>0){
									//alert($(".company-box .company").length);
									statistics($("#dtp_input1").val(), $(
									"#dtp_input2").val());
								//}
								
							}
						});
	};
	var companyWork2 = function() {
		$(".company-box .company i").unbind().click(function() {
			$(this).closest("div").hide(300, function() {
				$(this).closest("div").remove();
				if ($(".company-box .company").length >= 0)
					statistics($("#dtp_input1").val(), $("#dtp_input2").val());
			});

			// if($(".company-box .company").length<5) $(".company-box
			// .companyAdd").removeClass("no");
		});
	};

	// 公司查询 end
	var statistics = function(timeStat, timeEnd) {
		companyName = "";
		$("#Companyx .boxTab").html("");
		for ( var i = 0; i < $(".company-box .company").length; i++) {
			companyName = companyName
					+ $(".company-box .company:eq(" + i + ")")
							.attr("data-code") + ",";
			$("#Companyx .boxTab").append(
					"<span data-code='"
							+ $(".company-box .company:eq(" + i + ")").attr(
									"data-code")
							+ "' title='"
							+ $(".company-box .company:eq(" + i + ")").attr(
									"title")
							+ "' >"
							+ $(".company-box .company:eq(" + i + ")").attr(
									"title") + "</span>");
		}
		if (companyName.length >= 0) {
			$(".boxTab span").removeClass("yes");
			$("#Orderx .boxTab span:eq(0)").addClass("yes");
			$("#Companyx .boxTab span:eq(0)").addClass("yes");
			TabWork();
			companyWork2();
			jc1(timeStat, timeEnd, companyName, $("#Orderx .boxTab span.yes")
					.attr("data-code"));
			jc2(timeStat, timeEnd, $("#Companyx .boxTab span.yes").attr(
					"data-code"));
			
		}
	};

	var TabWork = function() {
		$("#Orderx .boxTab span").unbind().click(
				function() {
					if (!$(this).hasClass("yes")) {
						$("#Orderx .boxTab span.yes").removeClass("yes");
						$(this).addClass("yes");
						jc1($("#dtp_input1").val(), $("#dtp_input2").val(),
								companyName, $("#Orderx .boxTab span.yes")
										.attr("data-code"));
					}
				});

		$("#Companyx .boxTab span").unbind()
				.click(
						function() {
							if (!$(this).hasClass("yes")) {
								$("#Companyx .boxTab span.yes").removeClass(
										"yes");
								$(this).addClass("yes");
								jc2($("#dtp_input1").val(), $("#dtp_input2")
										.val(), $("#Companyx .boxTab span.yes")
										.attr("data-code"));
								
							}
						});

	};
	var jc1 = function(timeStat, timeEnd, companyName, tab) {// companyName
		// 公司code="11,12,13,"
		// tab 1订单数量 2 商品金额
		// 3商品数量

		// 图表1
		var myChart = echarts.init(document.getElementById('main10_1'));
		myChart.showLoading({
			text : '正在努力的读取数据中...'
		});

		var option = {
			title : {

			},
			tooltip : {
				trigger : 'axis'
			},
			legend : {
				data : [],
				textStyle: {color: '#5a8791'}
			},

			calculable : true,
			dataZoom : {
				show : true,
				realtime : true,
				start : 0,
				end : 100
			},
			xAxis : [ {
				type : 'category',
				boundaryGap : false,
				axisLabel:{
	        		show : true,
	        		textStyle:{
		        		color:'#93a4ab',
		        	},
	        	},
				splitLine:{
	        		 lineStyle : {  
						color: ['#364447'],
	                    width: 1,
	                    type : 'solid'  
	                    }  
	        	},
				data : []
			} ],
			yAxis : [  {
	            type : 'value',	
				axisLine: {
						lineStyle: {
							color: '#364447'
						}
					},
				axisLabel:{
	        		show : true,
	        		textStyle:{
		        		color:'#93a4ab',
		        	},
	        	},
	        	splitLine:{
	        		 lineStyle : {  
						color: ['#495f64'],
	                    width: 1,
	                    type : 'solid'  
	                    }  
	        	},
	        	show : true
	        }],
			series : []
		};

		// 为echarts对象加载数据
		myChart.setOption(option);
		// echarts加载图形
		myChart.hideLoading();

		// 图形加载所需的数据
		var viewData = null;
		var timeSectionValue = null;
		if ($(".time  .timeBtn.yes").length > 0) {
			timeSectionValue = $(".time  .timeBtn.yes").attr("data-section");
		}

		// 刷新图型函数

		$.ajax({
			url : 'getcompanydata.htm',
			type : 'POST',
			dataType : 'json',
			data : {
				startTime : timeStat,
				endTime : timeEnd,
				companyCode : companyName,
				orderByEnum : tab,
				timeSection : timeSectionValue

			},
			async : true,
			success : function(result) {
				if (null == result) {
					$( "#dialog" ).html("");
					$( "#dialog" ).html("<p>无数据</p>");
					$( "#dialog" ).dialog();
				}
				if (result.success) {
					var objectZhexianData = result.objectZhexian;
					option.legend.data = objectZhexianData.legendArray;
					option.xAxis[0].data = objectZhexianData.nameArray;
					option.series = objectZhexianData.zhexiandata;
					// 图形数据清理
					myChart.clear();
					// 为echarts对象加载数据
					myChart.setOption(option);
					// echarts加载图形
					myChart.hideLoading();
				} else {
					$( "#dialog" ).html("");
					$( "#dialog" ).html("<p>"+result.message+"</p>");
					$( "#dialog" ).dialog();
				}

			},
			error : function() {
				myChart.hideLoading();
			}
		});

	};

	var jc2 = function(timeStat, timeEnd, companyCodea) {// 订单商品品类分析
		// 基于准备好的dom，初始化echarts图表
		var myChart = echarts.init(document.getElementById('main12_1'));
		myChart.showLoading({
			text : '正在努力的读取数据中...'
		});

		var option = {
			title : {

			},
			tooltip : {
				trigger : 'item',
				formatter : "{a} <br/>{b} : {c} ({d}%)"
			},
			legend : {
				orient : 'vertical',
				x : 'left',
				data : []
			},
			calculable : true,
			series : [ {
				name : '订单商品分类分析',
				type : 'pie',
				radius : [ '30%', '50%' ],
				itemStyle : {
					normal : {
						color : function(params) {
							// build a color map as your need.
							var colorList = [ '#02b9f2', '#f94546', '#fad36a',
									'#a88cd6', '#3db5ac', '#f68411' ];
							return colorList[params.dataIndex];
						},

					},

				},
				data : []
			} ]
		};

		// 为echarts对象加载数据
		myChart.setOption(option);
		// echarts加载图形
		myChart.hideLoading();

		// 图形加载所需的数据
		var viewData = null;
		// 图形加载一次
		var timeSectionValue = null;
		if ($(".time  .timeBtn.yes").length > 0) {
			timeSectionValue = $(".time  .timeBtn.yes").attr("data-section");
		}
		// 刷新图型函数
		
			$.ajax({
				url : 'getcompanydata.htm',
				type : 'POST',
				dataType : 'json',
				data : {
					startTime : timeStat,
					endTime : timeEnd,
					companyCode : companyCodea,
					
					timeSection : timeSectionValue

				},
				async : true,
				success : function(result) {
					if (null == result) {
						$( "#dialog" ).html("");
						$( "#dialog" ).html("<p>无数据</p>");
						$( "#dialog" ).dialog();
					}
					if (result.success) {
						var objectOtherGraphData = result.objectOtherGraph;
						var objectPieData=objectOtherGraphData.pieData;
						option.legend.data = objectPieData.pieLegendArray;
						
						option.series[0].data = objectPieData.pieDataArray;
						// 图形数据清理
						myChart.clear();
						// 为echarts对象加载数据
						myChart.setOption(option);
						// echarts加载图形
						myChart.hideLoading();
					} else {
						$( "#dialog" ).html("");
						$( "#dialog" ).html("<p>"+result.message+"</p>");
						$( "#dialog" ).dialog();
					}

				},
				error : function() {
					myChart.hideLoading();
				}
			});
		
	};

	setQtime(6);// 初始7天
});